package com.aaa.marketresearch.webclient.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author Nail
 */

@Controller
public class UserController {
/*
    private static final Logger log = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private AuthenticationManager authenticationManager;

    private static void initJSONHeader(final HttpServletResponse httpResponse) {
        httpResponse.setHeader("Connection", "close");
        httpResponse.setHeader("Content-Type", "application/json");
        httpResponse.setHeader("Expires", "Thu, 19 Nov 1981 08:52:00 GMT");
        httpResponse.setHeader("Cache-Control", "no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
        httpResponse.setHeader("Pragma", "no-cache");
        httpResponse.setCharacterEncoding("UTF-8");
    }

    @RequestMapping(value = "urlLogin")
    void urlLogin(final HttpServletRequest httpRequest, final HttpServletResponse httpResponse) {
        String name = httpRequest.getParameter("name");
        String pass = httpRequest.getParameter("pass");

        Authentication authenticationToken = new UsernamePasswordAuthenticationToken(name, pass);
        Authentication authentication = this.authenticationManager.authenticate(authenticationToken);
        SecurityContextHolder.getContext().setAuthentication(authentication);
        try {
            initJSONHeader(httpResponse);
            PrintWriter out = httpResponse.getWriter();
            out.print(authentication.getName());
            out.flush();
            out.close();
        } catch (IOException ex) {
            log.error("urlLogin {}", ex);
        }
    }

    @RequestMapping(value = "test")
    void test(final HttpServletRequest httpRequest, final HttpServletResponse httpResponse) {
        String name = httpRequest.getParameter("name");
        String pass = httpRequest.getParameter("pass");

        try {
            initJSONHeader(httpResponse);
            PrintWriter out = httpResponse.getWriter();
            out.print("name: " + name + ", pass " + pass);
            out.flush();
            out.close();
        } catch (IOException ex) {
            log.error("urlLogin {}", ex);
        }
    }
*/
}